package com.spring.campus_express_delivery.controller;

import com.spring.campus_express_delivery.dao.ExpressTypeDao;
import com.spring.campus_express_delivery.entity.ExpressCompany;
import com.spring.campus_express_delivery.entity.ExpressType;
import com.spring.campus_express_delivery.entity.PageBean;
import com.spring.campus_express_delivery.entity.Users;
import com.spring.campus_express_delivery.interceptor.RequiredPermission;
import com.spring.campus_express_delivery.service.ExpressTypeService;
import com.spring.campus_express_delivery.util.JwtToken;
import com.spring.campus_express_delivery.util.MyUtil;
import com.spring.campus_express_delivery.util.RedisUtil;
import com.spring.campus_express_delivery.util.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@CrossOrigin
@Controller
@RequestMapping("/express_type")
@Api(value = "ExpressTypeController", description = "快递重量类型相关api")
public class ExpressTypeController {
    Boolean flag;
    MyUtil myUtil = new MyUtil();
    @Autowired
    ExpressTypeService expressTypeService;
    @Autowired
    RedisUtil redisUtil;
    /**
     * 快递重量类型列表
     * @param size
     * @param page
     * @param sort
     * @param asc
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("快递重量类型列表")
    @GetMapping("/wx/expressWeightType_list")
    @ResponseBody
    public ResponseEntity selectExpressWeightTypeByPage(@RequestParam(value = "size",required = false,defaultValue = "10") int size,
                                      @RequestParam(value = "page",required = false,defaultValue = "1") int page,
                                      @RequestParam(value = "sort",required = false) String sort,
                                      @RequestParam(value = "asc",required = false) String asc,
                                      @RequestHeader String Authorization) throws IOException {

        ResponseEntity responseEntity;
        String token = Authorization;
        PageBean<ExpressType> pagemsg = expressTypeService.selectExpressWeightTypeByPage(size, page, sort, asc);
        responseEntity = ResponseEntity.ok();
        responseEntity.putDataValue("records",pagemsg);
        return responseEntity;
    }
    /**
     * 添加快递重量类型
     * @param orderType_price
     * @param expressWeightType
     * @param Authorization
     * @throws IOException
     * @rueturn
     */
    @ApiOperation("添加快递重量类型")
    @PostMapping("/web/insertExpressWeightType")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity insertExpressWeightType(@RequestParam(required = true) String expressWeightType,@RequestParam(required = true)Double orderType_price, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        Users users = JwtToken.unsign(token, Users.class);
        ExpressType expressType1 = expressTypeService.selectByExpressWeightType(expressWeightType);
        if (expressType1 == null) {
            responseEntity = ResponseEntity.ok();
            ExpressType expressType = new ExpressType();
            expressType.setExpressWeightType(expressWeightType);
            expressType.setOrderType_price(orderType_price);
            expressTypeService.insertExpressWeightType(expressType);
            responseEntity.putDataValue("msg", "添加成功");
            return responseEntity;
        } else {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "快递重量类型已存在无法添加");
            return responseEntity;
        }

    }

    /**
     * 修改快递重量类型
     *
     * @param id
     * @param expressWeightType
     * @param orderType_price
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("修改快递重量类型")
    @PostMapping("/web/updateExpressWeightType")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity updateExpressWeightType(@RequestParam(required = true) int id, @RequestParam(required = false) String expressWeightType,@RequestParam(required = false) Double orderType_price, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        Users users = JwtToken.unsign(token, Users.class);
        responseEntity = ResponseEntity.ok();
        ExpressType expressType = expressTypeService.selectByExpressWeightId(id);
        if (expressType.getExpressWeightType() != expressWeightType) {
            expressType.setExpressWeightType(expressWeightType);
            expressType.setOrderType_price(orderType_price);
            expressTypeService.updateExpressWeightType(expressType);
            responseEntity.putDataValue("msg", "修改成功");
            //删除缓存
            String key = "expressType_" + id;
            if (redisUtil.hasKey(key)) {
                redisUtil.del(key);
            }
            return responseEntity;
        } else {
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg", "快递重量类型已存在，修改成另一个快递类型");
            return responseEntity;
        }

    }

    /**
     * 根据id查询快递重量类型
     *
     * @param id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据id查询快递重量类型")
    @GetMapping("/web/selectByExpressWeightId")
    @ResponseBody
    public ResponseEntity selectByExpressWeightId(@RequestParam(required = true) Integer id, @RequestHeader String Authorization) throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        Users users = JwtToken.unsign(token, Users.class);
        //查询缓存
        String key = "expressType_" + id;
        boolean b = redisUtil.hasKey(key);
        //如果redis中存在 查询缓存
        if (b){
            ExpressType expressType = (ExpressType) redisUtil.get(key);
            responseEntity=ResponseEntity.ok();
            responseEntity.putDataValue("expressType",expressType);
            return responseEntity;
        }
        //redis中没有缓存，从数据库中查询
        responseEntity=ResponseEntity.ok();
        ExpressType expressType = expressTypeService.selectByExpressWeightId(id);
        responseEntity.putDataValue("expressType",expressType);
        //将从数据库查到的数据缓存到redis
        redisUtil.set("expressType_" + id, expressType);
        return responseEntity;
    }



    /**
     * 根据id删除快递重量类型
     * @param id
     * @param Authorization
     * @return
     * @throws IOException
     */
    @ApiOperation("根据id删除快递重量类型")
    @DeleteMapping("/web/deleteExpressWeightById")
    @ResponseBody
    @RequiredPermission(1)
    public ResponseEntity deleteExpressWeightById(@RequestParam(required = true) Integer id,@RequestHeader String Authorization)throws IOException {
        ResponseEntity responseEntity;
        String token = Authorization;
        Users users = JwtToken.unsign(token, Users.class);
        ExpressType expressType = expressTypeService.selectByExpressWeightId(id);
        if (expressType != null) {
            expressTypeService.deleteExpressWeightById(id);
            responseEntity = ResponseEntity.ok();
            responseEntity.putDataValue("msg", "删除成功");
            //删除缓存
            String key = "expressType_" + id;
            if (redisUtil.hasKey(key)) {
                redisUtil.del(key);
            }
            return responseEntity;
        }else{
            responseEntity=ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","快递重量类型不存在，删除失败");
            return responseEntity;
        }
    }

}
